package com.li.algorithm.leetcode.hot100;


public class Test27 {

    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        //TODO:同时遍历两个链表，将两个链表中的元素升序串到一个链表里
        ListNode head = new ListNode(0);
        ListNode p = head;
        while (list1 != null && list2 != null) {
            if (list1.val < list2.val) {
                p.next = list1;
                list1 = list1.next;
                p = p.next;
            } else {
                p.next = list2;
                list2 = list2.next;
                p = p.next;
            }
        }
        p.next = list1 == null ? list2 : list1;
        return head.next;
    }


    public static void main(String[] args) {
        Test27 test27 = new Test27();
        ListNode a = new ListNode(4);
        ListNode b = new ListNode(2, a);
        ListNode c = new ListNode(1, b);

        ListNode d = new ListNode(4);
        ListNode e = new ListNode(3, d);
        ListNode f = new ListNode(1, e);

        ListNode node = test27.mergeTwoLists(c, f);
        while (node != null) {
            System.out.println(node.val);
            node = node.next;
        }
    }

}
